    #include "rd_03e.h"

#define SAMPLE_UART_NAME       "uart2"    /* 串口设备名称 */
static rt_device_t serial;                /* 串口设备句柄 */

struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */
static struct rt_semaphore rx_sem;    /* 用于接收消息的信号量 */

/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(&rx_sem);

    return RT_EOK;
}

static void RD_03_text_entry(void *parameter)
{
    rt_err_t device_state=RT_EOK;
    /* 查找串口设备 */
    serial = rt_device_find(SAMPLE_UART_NAME);
    if(serial==RT_NULL)
    {
        rt_kprintf("RD_03E find error:%d\n",device_state);
    }

    config.baud_rate = 256000;        //修改波特率为 9600
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 128;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位

    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

    device_state=rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);/* 以 DMA 接收及轮询发送模式打开串口设备 */
    if(device_state!=RT_EOK)
    {
        rt_kprintf("RD_03E open error:%d\n",device_state);
    }

    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);

    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial, uart_input);

    rt_uint8_t rd_03_date[7]={0};
    for (int j = 0; j < 200; ++j)
    {
        for (int var = 0; var < 7; ++var) {
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
            rt_device_read(serial, -1, &rd_03_date[var], 1);
        }
/*
        rt_kprintf("\n");
        rt_kprintf("date:");
        rt_kprintf("\t");
        for (int var = 0; var < 7; ++var) {
            rt_kprintf("%x\t",rd_03_date[var]);
        }
        rt_kprintf("\n");*/
        if((rd_03_date[0]==0xaa&&rd_03_date[1]==0xaa)&&(rd_03_date[5]==0x55&&rd_03_date[6]==0x55))
        {
            if(rd_03_date[2]==0x00)
                rt_kprintf("no find person!\n");
            if(rd_03_date[2]==0x01)
            {
                rt_kprintf(" find run person!\n");
                rt_kprintf("person distance :%d\n",((rd_03_date[4]<<8)|rd_03_date[3]));
            }
            if(rd_03_date[2]==0x02)
            {
                rt_kprintf(" find  person!\n");
                rt_kprintf("person distance :%d\n",((rd_03_date[4]<<8)|rd_03_date[3]));
            }

        }
        for (int var = 0; var < 7; ++var) {
           rd_03_date[var]=0;
        }

    }
    rt_device_close(serial);
}

static int RD_03_sample(void)
{
    rt_thread_t distance_thread;

    distance_thread = rt_thread_create("RD_03",
                                    RD_03_text_entry,
                                     RT_NULL,
                                     1024,
                                     RT_THREAD_PRIORITY_MAX / 2,
                                     20);
    if (distance_thread != RT_NULL)
    {
        rt_thread_startup(distance_thread);
    }

    return RT_EOK;
}
MSH_CMD_EXPORT(RD_03_sample , RD_03_text);
